package com.ming.utils;

import android.app.Activity;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;


public class ViewHelper {

    /**
     * 获取activity的根view
     */
    public static View getActivityRoot(Activity activity) {
        return ((ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT)).getChildAt(0);
    }


    /**
     * 设置view 是否选中
     *
     * @param view
     */
    public static void setViewChecked(CompoundButton view, boolean isChecked) {
        if (view == null) {
            return;
        }
        view.setChecked(isChecked);
    }

    /**
     * 设置view 是否可进行操作
     *
     * @param view
     * @param isEnabled
     */
    public static void setViewEnabled(View view, boolean isEnabled) {
        if (view == null) {
            return;
        }
        view.setEnabled(isEnabled);
    }

    /**
     * 设置view是否可见
     *
     * @param view
     * @param visibility
     */
    public static void setVisibility(View view, @Visibility int visibility) {
        if (view == null) {
            return;
        }
        view.setVisibility(visibility);
    }

    @IntDef({VISIBLE, INVISIBLE, GONE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Visibility {
    }

    /**
     * 设置textView 文字
     *
     * @param textView
     * @param text
     */
    public static void setHint(TextView textView, CharSequence text) {
        if (textView == null) {
            return;
        }
        if (text == null || text.length() == 0 || TextUtils.isEmpty(text.toString())) {
            return;
        }
        textView.setHint(text);
    }

    /**
     * 中间线
     *
     * @param textView
     */
    public static final void setCenterLine(TextView textView) {
        if (textView == null) {
            return;
        }
        textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        textView.getPaint().setAntiAlias(true);//抗锯齿
    }

    /**
     * 下划线
     *
     * @param textView
     */
    public static final void setUnderLine(TextView textView) {
        if (textView == null) {
            return;
        }

        textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        textView.getPaint().setAntiAlias(true);//抗锯齿
    }


    /**
     * 设置textView 文字
     *
     * @param textView
     * @param text
     */
    public static void setText(TextView textView, CharSequence text) {
        setText(textView, text, "");
    }

    /**
     * 设置textView 文字  带默认值
     *
     * @param textView
     * @param text
     */
    public static void setText(TextView textView, CharSequence text, String defaultTxt) {
        if (textView == null) {
            return;
        }
        if (text == null || text.length() == 0 || TextUtils.isEmpty(text.toString())) {
            textView.setText(defaultTxt);
            return;
        }
        textView.setText(text);
    }


    /**
     * 设置textView 文字
     *
     * @param textView
     * @param txt
     */
    public static void setTextHint(TextView textView, String txt) {
        setTextHint(textView, txt, "");
    }

    /**
     * 设置textView 文字  带默认值
     *
     * @param textView
     * @param txt
     */
    public static void setTextHint(TextView textView, String txt, String defaultTxt) {
        if (textView == null) {
            return;
        }
        if (TextUtils.isEmpty(txt)) {
            textView.setText(defaultTxt);
        }
        textView.setHint(txt);
    }

    /**
     * 设置textView 字体颜色
     *
     * @param textView
     * @param color
     */
    public static final void setTextColor(TextView textView, int color) {
        if (textView == null) {
            return;
        }
        textView.setTextColor(color);
    }

    /**
     * 获取textVeiw 以及其子类的 文字
     *
     * @param textView
     * @return
     */
    public static String getText(TextView textView) {
        if (textView == null) {
            return "";
        }
        return textView.getText().toString().trim();
    }


    public static final void setImageViewRes(ImageView imageView, @DrawableRes int resId) {
        if (imageView == null) {
            return;
        }
        imageView.setImageResource(resId);
    }

    public static final void setBackgroudRes(View view, @DrawableRes int resId) {
        if (view == null) {
            return;
        }
        view.setBackgroundResource(resId);
    }


    public static final void setBackgroudColor(View view, @ColorInt int resId) {
        if (view == null) {
            return;
        }
        view.setBackgroundColor(resId);
    }


    public static RectF getRectOnScreen(View view) {
        if (view == null) {
            Log.e("ViewUtils", "Please pass non-null referParent and child.");
            return new RectF();
        }

        RectF result = new RectF();

        int[] pos = new int[2];
        view.getLocationOnScreen(pos);

        result.left = pos[0];
        result.top = pos[1];
        result.right = result.left + view.getMeasuredWidth();
        result.bottom = result.top + view.getMeasuredHeight();
        return result;
    }

    /**
     * destroy 浏览器 信息
     *
     * @param webView
     */
    public static void destroyWebView(WebView webView) {
        if (webView == null) {
            return;
        }
        //清除历史记录
        webView.clearHistory();
        webView.clearCache(true);
        webView.loadUrl("about:blank"); // clearView() should be changed to loadUrl("about:blank"), since clearView() is deprecated now
        webView.freeMemory();
        webView.pauseTimers();
        webView.destroy();
        webView = null; // Note that mWebView.destroy() and mWebView = null do the exact same thing
    }

}
